package systempen.cmanager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Dialog;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class DialogAccountBrowser extends Dialog implements OnClickListener {
	Button okButton;
	Button cancelButton;
	ListView lw;
	String account;
	IFoundAccount ifo;
	public OnItemClickListener kl;
	public DialogAccountBrowser(Context context, IFoundAccount iFoundCos) {
		super(context);
		ifo = iFoundCos;
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.dialog_account_browser);
		lw = (ListView) findViewById(R.id.dialog_account_browser_list);
		lw.setOnItemClickListener(kl);
		fillList();
		
	}
	@Override
	public void onClick(View v) {
	/** When OK Button is clicked, dismiss the dialog */
	if (v == cancelButton)
	dismiss();
	if (v==okButton){
		if (account.length() ==0){			
		Toast.makeText(getContext(),"Level can't be empty.", Toast.LENGTH_LONG).show();
		dismiss();
		}
		onFoundAccount(account);
		dismiss();
	}
		else{
			//
			dismiss();
			onFoundAccount("0");
		}		
	}
	public void fillList(){
		Cursor c;
		ArrayList<ListItemDialogTypeBrowser> m_Devices = new ArrayList<ListItemDialogTypeBrowser>();
        List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
        util u = new util();
        c = u.getCursor("select description as name, a as _id, a as code from account ",getContext());
		c.moveToFirst();
		//int i = c.getCount();
		if (c.getCount()>0){
		 if (c.moveToFirst()) { 
			 //ArrayList<String> strings = new ArrayList<String>();
				ListItemDialogTypeBrowser device;
			 do {
				 //String mC = c.getString(0);
				 HashMap<String, String> map = new HashMap<String, String>();
				 map.put("name",c.getString(0));
				 String numberOfSubrtpes = util.getSubtypes(c.getString(2).toString(),getContext());
				 if (numberOfSubrtpes  == null)
						 numberOfSubrtpes = "0";
				 map.put("code",c.getString(2));
				 fillMaps.add(map);
				 //strings.add(mC); //cemu ovo sluzi?
				 String code  = c.getString(2);
				 String opis = c.getString(0);
				 device = new ListItemDialogTypeBrowser(code,opis);
				 m_Devices.add(device);
				 }while (c.moveToNext()); 
			 } 
		 }
		c.close();
		u.db.close();
		ListAdapterDialogTypeBrowser lvAdapter =  new ListAdapterDialogTypeBrowser(getContext(), R.layout.item_dialog_type_browser, m_Devices);
		//setListAdapter(lvAdapter);
		lw.setAdapter(lvAdapter);//valjda je ovo?
		kl = new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				View name = ((LinearLayout) arg1).getChildAt(0);
				String sName = ((TextView)name).getText().toString();
				//Toast.makeText(getContext(),"Costtypes: FFS: "+sName, Toast.LENGTH_LONG).show();
				//Toast.makeText(getContext(),"Zis one has not subtypes "+sName, Toast.LENGTH_SHORT).show();
				account = sName;
				onFoundAccount(account);
				dismiss();
			}
		};
		lw.setOnItemClickListener(kl);
	}
	public interface IFoundAccount{
		public void onFoundAccount(String account);
	}
	public void onFoundAccount(String account){
		if (ifo!=null){
			ifo.onFoundAccount(account);
		}
		
	}

}
